package com.partners.controller;

import com.partners.annotation.BeanValid;
import com.partners.annotation.SimpleParam;
import com.partners.constant.ErrorMessageEnum;
import com.partners.dao.dto.PermissionOutputDTO;
import com.partners.dao.dto.RolePermissionDTO;
import com.partners.dao.bo.PermissionDO;
import com.partners.dao.bo.RolePermissionDO;
import com.partners.model.ApiResult;
import com.partners.model.BusinessException;
import com.partners.model.Paging;
import com.partners.service.PermissionService;
import com.partners.utils.ApiResultUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 权限管理
 * @author liangyongle
 * @date 2018-12-08
 */

@RequestMapping("permission")
@RestController
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    /**
     * 前端用户的权限列表
     *
     * */
    @PostMapping("mypermission")
    public ApiResult<List<PermissionOutputDTO>> getMyPermissionList(@SimpleParam("uid") Integer uid){
         List<PermissionOutputDTO>  list = permissionService.getMyPermissionList(uid);
         return ApiResultUtil.ok(list);
    }

    /**
     * 获取权限列表
     * @param uid  Integer paging Paging
     * @return Object
     */
    @PostMapping("list")
    public ApiResult getPermissionList(
        @BeanValid @RequestBody Paging paging,@SimpleParam("uid") Integer uid){
        if(uid == null){
            throw  BusinessException.of(ErrorMessageEnum.UID_NOT_FOUND);
        }
        Map<String,Object> list = permissionService.getPermissionList(paging,uid);
        return ApiResultUtil.ok(list);
    }


    /**
     * 获取权限详情
     * @param id Integer
     * @return Object
     */
    @PostMapping("permission_detail")
    public ApiResult getPermissionDetail(@SimpleParam("id") Integer id){
        if(id == null){ throw BusinessException.of(ErrorMessageEnum.ID_NOT_FOUND);}
        PermissionDO permissionDO = permissionService.getPermissionDetail(id);
        return ApiResultUtil.ok(permissionDO);
    }


    /**
     * 编辑权限
     * @param permissionDO PermissionDO
     * @return Object
     */
    @PostMapping("permission_edit")
    public ApiResult editPermission(@RequestBody PermissionDO permissionDO){
      Integer result = permissionService.editPermission(permissionDO);
      return ApiResultUtil.ok(result);
    }


    /**
     * 根据角色获取相应的权限列表
     * @param roleId Integer
     * @return Object
     */
    @PostMapping("role_prmission")
    public ApiResult rolePermission(@BeanValid @RequestBody Paging paging,@SimpleParam("role_id") Integer roleId){
        Map<String,Object> list = permissionService.getRolePermission(paging,roleId);
        return ApiResultUtil.ok(list);
    }

    /**
     * 根据角色获取相应的权限列表
     * @param rolePermissionDO RolePermissionDO
     * @return Object
     */
    @PostMapping("role_permission_edit")
    public ApiResult rolePermissionEdit(@RequestBody RolePermissionDO rolePermissionDO){
        Integer result = permissionService.editRolePermission(rolePermissionDO);
        return ApiResultUtil.ok(rolePermissionDO);
    }


    /**
     * 编辑角色
     * @param rolePermissionDTO RolePermissionDTO
     * @return Object
     */
    @PostMapping("role_per_edits")
    public ApiResult rolePerEdit(@RequestBody RolePermissionDTO rolePermissionDTO){
        Boolean result  = permissionService.editPerRole(rolePermissionDTO);
        return ApiResultUtil.ok(result);
    }

    /**
     * 删除权限
     * @param id Integer
     * @return Object
     */

    @PostMapping("permission_delete")
    public ApiResult deletePermission(@SimpleParam("id") Integer id){
       Integer result = permissionService.deletePermission(id);
        return ApiResultUtil.ok(result);
    }


    /**
     * 删除权限关系
     * @param roleId Integer pid Integer
     * @return Object
     */
    @PostMapping("role_permission_delete")
    public ApiResult deleteRolePermission(@SimpleParam("role_id") Integer roleId,@SimpleParam("pid") Integer pid){
        Integer result = permissionService.deleteRolePermission(roleId,pid);
        return ApiResultUtil.ok(result);
    }




//    /**
//     * 根据角色获取相应的权限列表
//     * @param roleId Integer
//     * @return Object
//     */
//    @PostMapping("role_permission_edit")
//    public ApiResult rolePermissionEdit(@RequestHeader("token") String token,
//                                        @RequestBody String body){
//        Map<String,Object> list = new HashMap<>();
//        Object js = JSONObject.parseObject(body);
//        String name =  ((JSONObject) js).getString("name");
//
//        list.put("token",token);
//        list.put("name",name);
//        return ApiResultUtil.ok(list);
//
//    }







}
